package com.tensquare.spit.service;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;
import java.util.List;

@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询所有
     * @return
     */
    public List<Spit> findAll() {
        return spitDao.findAll();
    }

    /**
     * 根据id查询
     */
    public Spit findById(String spitId) {
        return spitDao.findById(spitId).get();
    }

    /**
     * 添加
     */
    public void add(Spit spit) {
        spit.set_id(idWorker.nextId() + "");

        // 设置初始化数据
        spit.setPublishtime(new Date());
        spit.setVisits(0);
        spit.setShare(0);
        spit.setShare(0);
        spit.setThumbup(0);
        spit.setComment(0);
        spit.setState("1");

        // 如果当前添加的吐槽 有父节点，那么父节点的吐槽回复要“+1”
        if (null != spit.getParentid() && !"".equals(spit.getParentid())) {
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }

        spitDao.save(spit);
    }

    /**
     * 更新
     */
    public void update(Spit spit) {

        spitDao.save(spit);
    }

    /**
     * 根据id删除
     */
    public void deleteById(String spitId) {
        spitDao.deleteById(spitId);
    }

    /**
     * 根据上级ID查询吐槽数据（分页）
     */
    public Page<Spit> findByParentid(String parentid, Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return spitDao.findByParentid(parentid, pageable);
    }

    /**
     * 点赞
     * @param spitId
     */
    public void updateThumbup(String spitId) {
        // 方式一：效率有问题
        // Spit spit = spitDao.findById(spitId).get();
        // spit.setThumbup((null == spit.getThumbup() ? 0 : spit.getThumbup()) + 1);
        // spitDao.save(spit);

        // 方式二：使用原生Mongo命令来实现自增：需要用到MongoTemplate
        // db.spit.update({"_id":"1",{$inc:{thumbup:NumberInt(1)}}})
        Query query = new Query(); // 封装查询添加
        query.addCriteria(Criteria.where("_id").is(spitId)); // 设置查询条件
        Update update = new Update(); // 封装更新的值
        update.inc("thumbup", 1); // 更新的字段，步长
        mongoTemplate.updateFirst(query, update, "spit");
    }


}
